﻿using System.Linq;
using System.Web.Mvc;
using CRM.Database.Model;
using DevExpress.Web.Mvc;


namespace CRM.Controllers
{
    [Localize, CheckAccess]
    public class UsersController : Controller
    {
       // private Entities db = new Entities();

        public ViewResult Index()
        {
            var col = new UsersCollection();
            return View(col.UsersList);
        }

        public ViewResult Details(int id)
        {
            var col = new UsersCollection();
            Users users = col.GetItemById(id) as Users;
            return View(users);
        }

        public ActionResult RolesPartial(int id)
        {
            var col = new UsersCollection();
            Users users = col.GetItemById(id) as Users;
            return PartialView("RolesPartial", users);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult UsersInRolesPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] UsersInRoles updatedItem, int id)
        {
            var usersInRolesCol = new UsersInRolesCollection();
           // var usersInRoleItem = updatedItem;
            //usersInRoleItem.IdUser = DB.UsersInRoles.First(s => s.IdUsersInRole == updatedItem.IdUsersInRole).IdUser;
            //DB.UsersInRolesUpdate(usersInRoleItem);
            usersInRolesCol.Update(updatedItem,Request.LogonUserIdentity.Name);
            var col = new UsersCollection();
            Users users = col.GetItemById(id) as Users;
            return PartialView("RolesPartial", users);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult UsersInRolesPartialDelete([ModelBinder(typeof(DevExpressEditorsBinder))] UsersInRoles deletedItem)
        {
            var usersInRolesCol = new UsersInRolesCollection();
            UsersInRoles item = (UsersInRoles) usersInRolesCol.GetItemById(deletedItem.IdUsersInRole);
            int idUser = 0;
            if (item != null)
                if (item.IdUser != null) idUser = (int) item.IdUser;
            //DB.UsersInRolesDelete(deletedItem);

            usersInRolesCol.Delete(deletedItem);

            var col = new UsersCollection();
            Users users = col.GetItemById(idUser) as Users;
            return PartialView("RolesPartial", users);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult UsersInRolesPartialAdd([ModelBinder(typeof(DevExpressEditorsBinder))] UsersInRoles usersInRoleItem, int id)
        {
            var usersInRolesCol = new UsersInRolesCollection();
            usersInRoleItem.IdUser = id;
            usersInRolesCol.Add(usersInRoleItem, Request.LogonUserIdentity.Name);
            var col = new UsersCollection();
            Users users = col.GetItemById(id) as Users;
            return PartialView("RolesPartial", users);
        }




        //-------------------------------------------------------
        //public ActionResult Create()
        //{
        //    return View();
        //} 

        //
        // POST: /Users/Create

        //[HttpPost]
        //public ActionResult Create(Users users)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Users.AddObject(users);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");  
        //    }

        //    return View(users);
        //}
        
        ////
        //// GET: /Users/Edit/5
 
        //public ActionResult Edit(int id)
        //{
        //    Users users = db.Users.Single(u => u.IdUser == id);
        //    return View(users);
        //}

        ////
        //// POST: /Users/Edit/5

        //[HttpPost]
        //public ActionResult Edit(Users users)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Users.Attach(users);
        //        db.ObjectStateManager.ChangeObjectState(users, EntityState.Modified);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    return View(users);
        //}

        ////
        //// GET: /Users/Delete/5
 
        //public ActionResult Delete(int id)
        //{
        //    Users users = db.Users.Single(u => u.IdUser == id);
        //    return View(users);
        //}

        ////
        //// POST: /Users/Delete/5

        //[HttpPost, ActionName("Delete")]
        //public ActionResult DeleteConfirmed(int id)
        //{            
        //    Users users = db.Users.Single(u => u.IdUser == id);
        //    db.Users.DeleteObject(users);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        //protected override void Dispose(bool disposing)
        //{
        //    db.Dispose();
        //    base.Dispose(disposing);
        //}
    }
}